package com.ruoyi.student.controller;

        import java.util.List;
        import javax.servlet.http.HttpServletResponse;

        import cn.hutool.core.bean.BeanUtil;
        import com.ruoyi.student.domain.StudentAll;
        import com.ruoyi.student.service.IPlanService;
        import com.ruoyi.system.domain.StudentInformation;
        import com.ruoyi.system.service.IStudentInformationService;
        import org.apache.ibatis.annotations.Param;
        import org.apache.ibatis.logging.stdout.StdOutImpl;
        import org.springframework.beans.BeanUtils;
        import org.springframework.security.access.prepost.PreAuthorize;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;
        import com.ruoyi.common.annotation.Log;
        import com.ruoyi.common.core.controller.BaseController;
        import com.ruoyi.common.core.domain.AjaxResult;
        import com.ruoyi.common.enums.BusinessType;
        import com.ruoyi.student.domain.Student;
        import com.ruoyi.student.service.IStudentService;
        import com.ruoyi.common.utils.poi.ExcelUtil;
        import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生信息Controller
 *
 * @author ruoyi
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/student/student")
public class StudentController extends BaseController
{
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IStudentInformationService informationService;
    @Autowired
    private IPlanService planService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('student:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(Student student,@RequestHeader("userId") Long userId)
    {
        startPage();
        System.out.println(userId);
        List<Student> list = studentService.selectStudentList(student,userId);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('student:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student,@RequestHeader("userId") Long userId)
    {
        List<Student> list = studentService.selectStudentList(student,userId);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:student:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return success(studentService.selectStudentByStudentId(studentId));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('student:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student)
    {
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('student:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('student:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(studentService.deleteStudentByStudentIds(studentIds));
    }
    @GetMapping("/me")
    public AjaxResult queryMe(@RequestParam("id") Long id){
        Student student = studentService.selectStudentByUserId(id);
        StudentInformation information = informationService.selectStudentInformationByUid(id);
        StudentAll studentAll = new StudentAll();
        if(student != null) {
            BeanUtil.copyProperties(student,studentAll);
            if(information != null) {
                BeanUtil.copyProperties(information,studentAll);
            }
        }
        return success(studentAll);
    }
    @GetMapping("/meBySID")
    public AjaxResult queryBySId(@RequestParam("id") Long id){
        Student student = studentService.selectStudentByStudentId(id);
        System.out.println(student);
        StudentInformation information = informationService.selectStudentInformationByUid(student.getUid());
        StudentAll studentAll = new StudentAll();
        if(student != null) {
            BeanUtil.copyProperties(student,studentAll);
            if(information != null) {
                BeanUtil.copyProperties(information,studentAll);
            }
        }
        return success(studentAll);
    }
    @PostMapping("/studentAll")
    public AjaxResult studentAll(@RequestBody StudentAll studentAll){
        return success(studentService.addStudentAll(studentAll));
    }
    @GetMapping("/address")
    public AjaxResult getAddress(){
        return success(studentService.getAddress());
    }
    @GetMapping("/plan/{id}")
    public AjaxResult queryPlan(@PathVariable Long id){
        return success(planService.selectPlanByStudentId(id));
    }
}
